package com.lanou.musicbox.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TabHost;
import com.lanou.musicbox.R;
import com.lanou.musicbox.base.BaseFragment;
/**
 * Created by dllo on 15/10/24.
 */
public class MainFragment extends BaseFragment implements View.OnClickListener {
    private FragmentManager manager;
    private TabHost mTabHost;
    private FragmentTransaction transaction;
    private LinearLayout scan;

    // 以下是抽屉的定义内容
    private LinearLayout linear_main;
    private LinearLayout changeSkinID;
    private LinearLayout downloadID;
    private LinearLayout setUpID;
    private DrawerLayout drawerLayout;

    public MainFragment() {

    }

    public MainFragment(FragmentManager manager) {
        super();
        this.manager = manager;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.drawer, null);

        mTabHost = (TabHost) v.findViewById(android.R.id.tabhost);
        // 监听抽屉的整体布局防止点击事件冲突
        linear_main = (LinearLayout) v.findViewById(R.id.linear_main);
        // 抽屉的监听用来打开或关闭抽屉
        drawerLayout = (DrawerLayout) v.findViewById(R.id.drawerLayout);
        scan = (LinearLayout) v.findViewById(R.id.scan);
        changeSkinID = (LinearLayout) v.findViewById(R.id.changeSkinID);
        downloadID = (LinearLayout) v.findViewById(R.id.downloadID);
        setUpID = (LinearLayout) v.findViewById(R.id.setUpID);

        mTabHost.setup();
        //本地音乐设置
        TabHost.TabSpec tab0 = mTabHost.newTabSpec("");
        View tab0View = inflater.inflate(R.layout.tab_locality_music, null);
        tab0.setIndicator(tab0View);
        tab0.setContent(R.id.locality_music);
        mTabHost.addTab(tab0);
        //网络音乐设置
        TabHost.TabSpec tab1 = mTabHost.newTabSpec("");
        View tab1view = inflater.inflate(R.layout.tab_netork_music, null);
        tab1.setIndicator(tab1view);
        tab1.setContent(R.id.network_music);
        mTabHost.addTab(tab1);
        //电台设置
        TabHost.TabSpec tab2 = mTabHost.newTabSpec("");
        View tab2view = inflater.inflate(R.layout.tab_network_radio, null);
        tab2.setIndicator(tab2view);
        tab2.setContent(R.id.network_radio);
        mTabHost.addTab(tab2);
        //搜素设置
        TabHost.TabSpec tab3 = mTabHost.newTabSpec("");
        View tab3view = inflater.inflate(R.layout.tab_seek_music, null);
        tab3.setIndicator(tab3view);
        tab3.setContent(R.id.seek_music);
        mTabHost.addTab(tab3);
        //设置第一个显示的页面
        mTabHost.setCurrentTab(0);
        //实例化 业务对象
        LocalityFragment localityFragment = new LocalityFragment();
        NetworkMusicFragment networkMusicFragment = new NetworkMusicFragment();
        NetworkRadioFragment networkRadioFragment = new NetworkRadioFragment();
        SeekMusicFragment seekMusicFragment = new SeekMusicFragment();
        transaction = getActivity().getSupportFragmentManager().beginTransaction();
        // 替换占位符
        transaction.replace(R.id.locality_music, localityFragment);
        transaction.replace(R.id.network_music, networkMusicFragment);
        transaction.replace(R.id.network_radio, networkRadioFragment);
        transaction.replace(R.id.seek_music, seekMusicFragment);
        //添加
        transaction.commit();

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        drawerLayout.setOnClickListener(this);
        linear_main.setOnClickListener(this);
        scan.setOnClickListener(this);
        changeSkinID.setOnClickListener(this);
        setUpID.setOnClickListener(this);
        downloadID.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.scan:
                mTabHost.setCurrentTab(0);
                break;
            case R.id.changeSkinID:
                mTabHost.setCurrentTab(1);
                break;
            case R.id.downloadID:
                mTabHost.setCurrentTab(2);
                break;
            case R.id.setUpID:
                mTabHost.setCurrentTab(3);
                break;
        }
        // 关闭抽屉
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        // 打开抽屉
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    }
}
